package com.chenglu.android.goodsinfo;

import android.content.Context;
import android.content.Intent;
import android.graphics.Outline;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenglu.android.R;
import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.cart.bean.CartBean;
import com.chenglu.android.common.base.BaseFullScreenActivity;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.BigDecimalUtil;
import com.chenglu.android.common.utils.GlideUtils;
import com.chenglu.android.common.utils.KeyboardUtils;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.StatusBarUtil;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.goodsinfo.adapter.CommentAdapter;
import com.chenglu.android.goodsinfo.adapter.GoodsRecommendAdapter;
import com.chenglu.android.goodsinfo.adapter.ShopFlowAdapter;
import com.chenglu.android.goodsinfo.adapter.ShopFlowItemAdapter;
import com.chenglu.android.goodsinfo.adapter.ShopFlowItemAdapter2;
import com.chenglu.android.goodsinfo.adapter.ShopFlowItemAdapter3;
import com.chenglu.android.goodsinfo.bean.GoodsCommentBean;
import com.chenglu.android.goodsinfo.bean.GoodsInfoBean;
import com.chenglu.android.goodsinfo.bean.ShopCouponPage;
import com.chenglu.android.goodsinfo.bean.ShopsinfoBean;
import com.chenglu.android.goodsinfo.infoutils.GoodsInfoUtils;
import com.chenglu.android.goodsinfo.mvp.contract.GoodsInfoContract;
import com.chenglu.android.goodsinfo.mvp.presenter.GoodsInfoPresenter;
import com.chenglu.android.goodsinfo.touch.BigImgActivity;
import com.chenglu.android.index.bean.IndexRecomBean;
import com.chenglu.android.mine.bean.CommonDataBean;
import com.chenglu.android.mine.login.LoginActivity;
import com.chenglu.android.payment.ComfirmOrderActivity;
import com.chenglu.android.payment.bean.CouponListBean;
import com.chenglu.android.payment.bean.OrderGoodsList;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.BottomPopupView;
import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: lyj
 * @Created: by Copyright 2023/9/25 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description： 餐饮的商品详情
 */
public class GoodsRestantInfoActivity extends BaseFullScreenActivity<GoodsInfoPresenter> implements GoodsInfoContract.View {

    public static final String ID = "id";
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_goods_name)
    TextView tvGoodsName;
    @BindView(R.id.tv_coupon)
    TextView tvCoupon;
    @BindView(R.id.tv_sell_number)
    TextView tvSellNumber;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.rl_gifts)
    RelativeLayout rlGifts;
    @BindView(R.id.rv_gift)
    RecyclerView rvGift;
    @BindView(R.id.ll_gifts)
    LinearLayout llGifts;
    @BindView(R.id.view_address)
    View viewAddress;
    @BindView(R.id.iv_shop_header)
    ImageView ivShopHeader;
    @BindView(R.id.cv_user)
    CardView cvUser;
    @BindView(R.id.tv_shopname)
    TextView tvShopname;
    @BindView(R.id.ratingBar_service)
    RatingBar ratingBarService;
    @BindView(R.id.tv_entering_store)
    TextView tvEnteringStore;
    @BindView(R.id.rt_shop_des)
    RelativeLayout rtShopDes;
    @BindView(R.id.rv_shop_recommend)
    RecyclerView rvShopRecommend;
    @BindView(R.id.rt_shop_recommend)
    RelativeLayout rtShopRecommend;
    @BindView(R.id.tv_goods_des)
    TextView tvGoodsDes;
    @BindView(R.id.x5webview)
    WebView x5webview;
    @BindView(R.id.view_shop_name)
    View viewShopName;
    @BindView(R.id.tv_goods_comment)
    TextView tvGoodsComment;
    @BindView(R.id.ll_comment_more)
    LinearLayout llCommentMore;
    @BindView(R.id.detail_sale_commpent)
    RelativeLayout detailSaleCommpent;
    @BindView(R.id.rv_comment)
    RecyclerView rvComment;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.ll_info)
    LinearLayout llInfo;
    @BindView(R.id.ic_back)
    ImageView icBack;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.btn_add_cart)
    Button btnAddCart;
    @BindView(R.id.btn_choose)
    Button btnChoose;
    @BindView(R.id.iv_collect)
    ImageView ivCollect;


    private CommentAdapter commentAdapter;
    private String id;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goods_restant_detail_layout);
        ButterKnife.bind(this);
        mPresenter = new GoodsInfoPresenter();
        mPresenter.attachView(this);
        StatusBarUtil.immersive(this);
        StatusBarUtil.setPaddingSmart(this, toolbar);

        id = getIntent().getStringExtra(ID);
        //商品信息
        mPresenter.getGoodsInfo(getMapParams(id));
        //商品评论
        mPresenter.getGoodsComment(getMapCommentParams(id));

//        mPresenter.getShopsRecommendList(Constant.one);


        initview1();


    }

    private void initview1() {
        //初始化
        initScroolview();
        //初始化评论
        initCommentRv();

        initRecommentRv();
    }


    private Map<String, String> getMapParams(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("id", id);

        return map;
    }


    //
    private Map<String, String> getMapCommentParams(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("current", "1");
        map.put("goodsId", id);
//        map.put("goodsReview", id);
        map.put("size", Constant.SIZE + "");

        return map;
    }

    List<GoodsCommentBean.Data.Records> goodsCommentLists = new ArrayList<>();

//    List<DetailInfoBean.Data.GoodsGiftList> goodsGiftLists = new ArrayList<>();

    //商品评论
    private void initCommentRv() {
        commentAdapter = new CommentAdapter(mContext, goodsCommentLists);
        rvComment.setLayoutManager(new LinearLayoutManager(mContext));
        rvComment.setAdapter(commentAdapter);

    }

    private GoodsRecommendAdapter recommentAdapter;
    List<IndexRecomBean.Data.Records> recommentlist = new ArrayList<>();

    //商品推荐
    private void initRecommentRv() {
        recommentAdapter = new GoodsRecommendAdapter(mContext, recommentlist);
        //
        rvShopRecommend.setLayoutManager(new GridLayoutManager(mContext, 3,
                LinearLayoutManager.VERTICAL, false));
        rvShopRecommend.setAdapter(recommentAdapter);


    }

    private GoodsInfoBean data;
    private boolean isCollect; //是否收藏

    @Override
    public void getGoodsInfo(GoodsInfoBean bean) {
        data = bean;

        isCollect = bean.getData().getIsCollect();

        tvPrice.setText(Utils.doubleTrans2(bean.getData().getGoodsPrice()));//价格
        tvGoodsName.setText(bean.getData().getGoodsName());//名称
        tvGoodsDes.setText(bean.getData().getGoodsDesc());
        //折扣大于才显示
        if (bean.getData().getDiscount() > 0) {
            String div = BigDecimalUtil.div(String.valueOf(bean.getData().getDiscount()), "10", 2);
            String discountPrice = BigDecimalUtil.mul(String.valueOf(bean.getData().getGoodsPrice()), div);
        }

        //显示网页数据
        initWebView(bean.getData().getGoodsDetail());

        List<String> images = new ArrayList();
        String[] split = bean.getData().getGoodsImgList().split(",");
        initBannerData(split, banner, images);

        //请求店铺信息
        mPresenter.getShopinfo(bean.getData().getTenantId());
        //商品列表
        mPresenter.getShopsRecommendList(bean.getData().getTenantId());


        tvSellNumber.setText("销量" + bean.getData().getSalesAmount() + "");

        if(bean.getData().getGoodsSpecs().size()<=0){
            btnAddCart.setVisibility(View.VISIBLE);
            btnChoose.setVisibility(View.GONE);
        }else {
            btnAddCart.setVisibility(View.GONE);
            btnChoose.setVisibility(View.VISIBLE);

        }

    }

    @Override
    public void getGoodsComment(GoodsCommentBean bean) {

        for (int i = 0; i < bean.getData().getRecords().size(); i++) {
            goodsCommentLists.add(bean.getData().getRecords().get(i));

        }
        commentAdapter.notifyDataSetChanged();
        if (goodsCommentLists.isEmpty()) {
            tvGoodsComment.setText("评价(" + goodsCommentLists.size() + ")");

            llCommentMore.setEnabled(false);
        } else {
            tvGoodsComment.setText("评价(" + goodsCommentLists.size() + ")");
            llCommentMore.setEnabled(true);

        }

    }

    @Override
    public void getShopinfo(ShopsinfoBean bean) {

        tvShopname.setText(bean.getData().getShopsName());
        ratingBarService.setRating(bean.getData().getScore());
//        ratingBarService.setNumStars(bean.getData().getScore());
        GlideUtils.setImageView(mContext, bean.getData().getLogoImage(), ivShopHeader);
    }

    //商品收藏
    @Override
    public void getGoodsCollectRemove(SmsCodeBean bean) {
        ToastUtils.show("取消收藏");
        isCollect = false;
//        ivCollectDefault.setBackground(mContext.getResources().getDrawable(R.drawable.icon_collect_default));
        data.getData().setIsCollect(false);
    }

    //店铺收藏
    @Override
    public void getShopsCollectRemove(SmsCodeBean bean) {
//        ToastUtils.show("取消收藏");
//        isCollect = false;
//        ivCollectDefault.setBackground(mContext.getResources().getDrawable(R.drawable.icon_collect_default));
//        data.getData().setIsCollect(false);
    }

    @Override
    public void getGoodsCollectSave(SmsCodeBean bean) {

        ToastUtils.show("收藏成功");
//        ivCollectDefault.setBackground(mContext.getResources().getDrawable(R.drawable.icon_collect_press));
        isCollect = true;
        data.getData().setIsCollect(true);
    }

    @Override
    public void getShopsRecommendList(IndexRecomBean bean) {
        for (int i = 0; i < bean.getData().getRecords().size(); i++) {
            if (i < 6) {
                recommentlist.add(bean.getData().getRecords().get(i));
            }
        }
        recommentAdapter.notifyDataSetChanged();
    }


    @Override
    public void getAddCartShop(CommonDataBean bean) {
        ToastUtils.show(bean.getMsg());
    }


//    购物车的数量
    @Override
    public void getShopsCart(CartBean bean) {

    }

    @Override
    public void getShopCouponPage(ShopCouponPage bean) {

    }

    @Override
    public void getShopCouponreceive(CommonDataBean bean) {

    }


    private void initWebView(String goodsdetail) {

        Utils.webX5init(x5webview);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开  防止加载网页时调起系统浏览器
        x5webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {

                view.loadUrl(goodsdetail);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView var1, SslErrorHandler handler, SslError var3) {
                handler.proceed(); //表示等待证书响应
                //handler.cancel(); //表示挂起连接，为默认方式
                // handler.handleMessage(null); //可做其他处理

            }
        });
//        x5webview.loadUrl(goodsdetail);
        x5webview.loadDataWithBaseURL(null, goodsdetail, "text/html", "UTF-8", null);

    }

    private int mOffset = 0;
    private int mScrollY = 0;

    private void initScroolview() {


        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            int lastScrollY = 0;
            int h = Global.dp2px(80);
            int color = ContextCompat.getColor(getApplicationContext(), R.color.white) & 0x00ffffff;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

                if (lastScrollY < h) {
                    scrollY = Math.min(h, scrollY);
                    mScrollY = scrollY > h ? h : scrollY;
//                    buttonBarLayout.setAlpha(1f * mScrollY / h);
                    toolbar.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
//                    banner.setTranslationY(mOffset - mScrollY);
                }
                if (scrollY == 0) {
                    tvTitle.setTextColor(getResources().getColor(R.color.transparent));
//                    llBlindBgsearch.setBackground(getResources().getDrawable(R.drawable.store_left_boom_shape_30));
//                    icBack.setBackgroundResource(R.drawable.ic_black_back);

                } else {
                    tvTitle.setTextColor(getResources().getColor(R.color.color_333333));
//                    llBlindBgsearch.setBackground(getResources().getDrawable(R.drawable.store_left_top_shape_30));
//                    icBack.setBackgroundResource(R.drawable.ic_black_back);
                    tvTitle.setText("商品详情");

                }

                lastScrollY = scrollY;
            }
        });
        toolbar.setBackgroundColor(0);


    }

    //设置banner数据
    private void initBannerData(String[] item, Banner banner, List<String> images) {

        for (int i = 0; i < item.length; i++) {
            images.add(item[i]);

        }
        //设置图片加载器
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });
        //设置图片集合
        banner.setImages(images);
        //设置轮播的动画效果
        banner.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        banner.isAutoPlay(false);
        //设置轮播时间（设置2.5秒切换下一张图片）
        banner.setDelayTime(2500);
        //设置banner显示样式（带标题的样式）
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //图片设置成圆角
        banner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 3);
            }
        });

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(mContext, BigImgActivity.class);
                intent.putStringArrayListExtra("imgData", (ArrayList<String>) images);
                intent.putExtra("clickPosition", position);
                startActivity(intent);

            }
        });
        banner.setClipToOutline(true);
        banner.start();
    }


    @OnClick({ R.id.ic_back, R.id.tv_title, R.id.ll_comment_more, R.id.iv_collect,R.id.btn_add_cart,R.id.btn_choose})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //加入购物车
            case R.id.btn_add_cart:
                break;
            //选规格
            case R.id.btn_choose:
                break;
            case R.id.tv_address:
                break;
            case R.id.ll_comment_more:
                startActivity(new Intent(this, CommentActivity.class)
                        .putExtra(CommentActivity.GoodsId, id));
                break;
            case R.id.iv_collect:
                if (isLogin) {
                    if (data != null) {
                        if (!isCollect) {
                            JSONObject jsonObject = new JSONObject();
                            try {
                                jsonObject.put("collectType", "1");
                                jsonObject.put("goodsId", id);
                                jsonObject.put("shopsTypeId", "1");
//                            jsonObject.put("collectType","1");
                                mPresenter.getGoodsCollectSave(getToken(), Utils.getRequestBody(jsonObject));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {

                            mPresenter.getGoodsCollectRemove(getToken(), id);
                        }
                    }
                } else {
                    startActivity(new Intent(mContext, LoginActivity.class));
                }

                break;
            case R.id.ic_back:
                finish();
                break;
            case R.id.tv_title:
                break;
        }
    }


    List<OrderGoodsList.OrderGoodsListData> orderGoodsListData = new ArrayList<>();

    //生成商超的订单传递参数
    private List<OrderGoodsList.OrderGoodsListData> getOrderGoodsList1(int amount) {

        OrderGoodsList.OrderGoodsListData data = new OrderGoodsList.OrderGoodsListData();

        data.setCategoryIds(this.data.getData().getCategoryIds());//分类ids
        data.setGoodsId(this.data.getData().getId());//商品id
        data.setGoodsName(this.data.getData().getGoodsName());//商品名称
        data.setIsSpec(this.data.getData().getIsSpec());//类别区分
//        orderGoodsListData.setGoodsSkuId(goodsInfoBean.getData().getGoodsSkus().get(0).getId());//类别区分
        data.setDiscountPrice(this.data.getData().getDiscountPrice());//类别区分
        data.setPrice(this.data.getData().getGoodsPrice());
        //设置数量
        data.setAmount(amount);
        if (allSkuId.equals("0")) {
            data.setGoodsSkuId("");
        } else {
            data.setGoodsSkuId(allSkuId);
        }
        orderGoodsListData.add(data);

        return orderGoodsListData;
    }


    /**
     * @description 加入购物车
     */

    private void addCartShop(String goodsId, String goodsName, String tenantId,
                             int shopsTypeId, int isSpec, int status, EditText etSearchGoods, int goodsSkuId) {

        if (isLogin) {
            //去请求数据
            //json传过去的数据
            JSONObject jsonObject = new JSONObject();
            try {

                jsonObject.put("goodsId", goodsId);
                jsonObject.put("goodsAmount", 1);
//                jsonObject.put("goodsAmount", etSearchGoods.getText().toString());
                jsonObject.put("goodsName", goodsName);
                jsonObject.put("tenantId", tenantId);
                jsonObject.put("shopsTypeId", shopsTypeId);
                jsonObject.put("isSpec", isSpec);
                jsonObject.put("status", status);

                if (isSpec == 1) {
                    jsonObject.put("goodsSkuId", goodsSkuId);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
            LogUtils.v("goodsAmount =" + 1 + "goodsid=" + goodsId + "giftid =" + goodsSkuId);
            mPresenter.getAddCartShop(getToken(), Utils.getRequestBody(jsonObject));

        } else {
            //去登录
            startActivity(new Intent(mContext, LoginActivity.class));
        }
    }


    //*******************商品详情加入购物车********************************//

    /**
     * @description 购物车的弹窗方法
     */

    CartBottomPopup cartBottomPopup;

    private void showCartShop() {
        cartBottomPopup = new CartBottomPopup(GoodsRestantInfoActivity.this);
        new XPopup.Builder(GoodsRestantInfoActivity.this)
                .animationDuration(200)
                .moveUpToKeyboard(false)
                .autoFocusEditText(false)
//                .autoOpenSoftInput(false)
                .enableDrag(false)
                .asCustom(cartBottomPopup)
                .show();
    }


    class CartBottomPopup extends BottomPopupView implements ShopFlowAdapter.ItemOnClickInterface {
        private EditText etCartGoodsNum;
        private TextView tvCartSkuSpec1;


        protected RecyclerView rvflow;
        protected RecyclerView rvflow2;
        protected RecyclerView rvflow3;
        protected RecyclerView rvflow4;

        protected ShopFlowItemAdapter shopFlowAdapter;

        protected ShopFlowItemAdapter2 shopFlowAdapter2;

        protected ShopFlowItemAdapter3 shopFlowAdapter3;
//        protected RecyclerView rvflow4;
//        protected RecyclerView rvflow4;
//        protected RecyclerView rvflow4;

        Button cartAddbtn; //加入购物车
        Button cartBuybtn; //立即购买
        private List<GoodsInfoBean.Data.GoodsSpecs.SpecValue> specValueList;

        private List<GoodsInfoBean.Data.GoodsSpecs.SpecValue> specValueList2;

        private List<GoodsInfoBean.Data.GoodsSpecs.SpecValue> specValueList3;

        private LinearLayout llAddBuy;

        //注意：自定义弹窗本质是一个自定义View，但是只需重写一个参数的构造，其他的不要重写，所有的自定义弹窗都是这样。
        public CartBottomPopup(@NonNull Context context) {
            super(context);
        }

        // 返回自定义弹窗的布局
        @Override
        protected int getImplLayoutId() {
            return R.layout.dialog_add_carts;
        }

        // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
        @Override
        protected void onCreate() {
            super.onCreate();


            ImageView ivCartGoodsImg = findViewById(R.id.iv_dialog_cart_goodsImg);
            ImageView ivDialogDelete = findViewById(R.id.iv_dialog_delete);
            TextView tvGoodsTotalPrice = findViewById(R.id.tv_cart_goodsTotalPrice);
            TextView tvGoodsSave = findViewById(R.id.tv_cart_goods_save);
            etCartGoodsNum = findViewById(R.id.et_cart_goodsNum);
            etCartGoodsNum.setText("1");

            RelativeLayout rlCartMinus = findViewById(R.id.rl_cart_minus);
            RelativeLayout rlCartAdd = findViewById(R.id.rl_cart_add);


            ImageView ivCartAdd = findViewById(R.id.iv_cart_add);

            ImageView ivCartMinus = findViewById(R.id.iv_cart_minus);


            //已选择库存显示
            tvCartSkuSpec1 = findViewById(R.id.tv_cart_sku_speckey1);

            //加入购物车
            cartAddbtn = findViewById(R.id.btn_add_shop);
            //立即购买
            cartBuybtn = findViewById(R.id.btn_buy_shop);


            rvflow = findViewById(R.id.rv_flow);
            rvflow2 = findViewById(R.id.rv_flow2);
            rvflow3 = findViewById(R.id.rv_flow3);
            rvflow4 = findViewById(R.id.rv_flow4);

            TextView headerTvShopFlow = findViewById(R.id.header_tv_shop_flow);
            TextView headerTvShopFlow2 = findViewById(R.id.header_tv_shop_flow2);
            TextView headerTvShopFlow3 = findViewById(R.id.header_tv_shop_flow3);
            TextView headerTvShopFlow4 = findViewById(R.id.header_tv_shop_flow4);

            LinearLayout llTagFlow1 = findViewById(R.id.ll_tag_flow);
            LinearLayout llTagFlow2 = findViewById(R.id.ll_tag_flow2);
            LinearLayout llTagFlow3 = findViewById(R.id.ll_tag_flow3);
            LinearLayout llTagFlow4 = findViewById(R.id.ll_tag_flow4);

            //无库存隐藏和显示
            llAddBuy = findViewById(R.id.ll_add_buy);


            if (data.getData().getGoodsSpecs().size() > 0 && data.getData().getGoodsSpecs().size() == 1) {

                initDialogShopflowRv(headerTvShopFlow, rvflow);

                llTagFlow1.setVisibility(VISIBLE);
                llTagFlow2.setVisibility(GONE);
                llTagFlow3.setVisibility(GONE);
                llTagFlow4.setVisibility(GONE);
                shopFlowAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                        setZeroEtNum(ivCartAdd);

                        //设置点中的item
                        shopFlowAdapter.setThisPosition(position);
                        //将选中的数值
                        checkPosition = position;
                        //拿到当前的值
                        GoodsInfoBean.Data.GoodsSpecs.SpecValue oneList = shopFlowAdapter.getData().get(position);
                        //"款式":"字母格" //拿第一个的
                        specValue = "{" + "\"" + data.getData().getGoodsSpecs().get(0).getSpecKey() + "\"" + ":" + "\"" + oneList.getValue() + "\"" + ",";

                        checkPositionShop(data, ivCartGoodsImg, tvGoodsTotalPrice, tvGoodsSave, tvCartSkuSpec1, cartAddbtn, btnName, llAddBuy);

//                        切换的数据
                        if (!TextUtils.isEmpty(etCartGoodsNum.getText().toString()) && Integer.parseInt(etCartGoodsNum.getText().toString()) > allSkuNumber) {
                            etCartGoodsNum.setText(allSkuNumber + "");
                        }
                    }
                });
            }


            if (data.getData().getGoodsSpecs().size() > 0 && data.getData().getGoodsSpecs().size() == 2) {
                llTagFlow1.setVisibility(VISIBLE);
                llTagFlow2.setVisibility(VISIBLE);
                llTagFlow3.setVisibility(GONE);
                llTagFlow4.setVisibility(GONE);
                //第一种rv
                initDialogShopflowRv(headerTvShopFlow, rvflow);

                shopFlowAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        setZeroEtNum(ivCartAdd);


//                        ToastUtils.show("点击了" + shopFlowAdapter.getData().get(position).getSpecValue());
                        //设置点中的item
                        shopFlowAdapter.setThisPosition(position);
                        //将选中的数值
                        checkPosition = position;
                        //拿到当前的值
                        GoodsInfoBean.Data.GoodsSpecs.SpecValue oneList = shopFlowAdapter.getData().get(position);
                        //"款式":"字母格"
                        specValue = "{" + "\"" + oneList.getKey() + "\"" + ":" + "\"" + oneList.getValue() + "\"" + ",";

                        checkPositionShop(data, ivCartGoodsImg, tvGoodsTotalPrice, tvGoodsSave, tvCartSkuSpec1, cartAddbtn, btnName, llAddBuy);
                        //切换的数据
                        if (!TextUtils.isEmpty(etCartGoodsNum.getText().toString()) && Integer.parseInt(etCartGoodsNum.getText().toString()) > allSkuNumber) {
                            etCartGoodsNum.setText(allSkuNumber + "");
                        }

                    }
                });

                //第二种的方式显示列表数据
                initDialogShopflowRv2(headerTvShopFlow2, rvflow2);

                shopFlowAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                        ToastUtils.show("点击了" + position);
                        //空 或者输入为0的时候
                        setZeroEtNum(ivCartAdd);

                        //设置点中的item
                        shopFlowAdapter2.setThisPosition(position);

                        checkPosition2 = position;

                        GoodsInfoBean.Data.GoodsSpecs.SpecValue goodList2 = shopFlowAdapter2.getData().get(position);

                        specValue2 = "\"" + goodList2.getKey() + "\"" + ":" + "\"" + goodList2.getValue() + "\"" + "}";

                        checkPositionShop(data, ivCartGoodsImg, tvGoodsTotalPrice, tvGoodsSave, tvCartSkuSpec1, cartAddbtn, btnName, llAddBuy);

                        //切换的数据
                        if (!TextUtils.isEmpty(etCartGoodsNum.getText().toString()) && Integer.parseInt(etCartGoodsNum.getText().toString()) > allSkuNumber) {
                            etCartGoodsNum.setText(allSkuNumber + "");
                        }

                    }
                });
            }

            if (data.getData().getGoodsSpecs().size() > 0 && data.getData().getGoodsSpecs().size() == 3) {
                llTagFlow1.setVisibility(VISIBLE);
                llTagFlow2.setVisibility(VISIBLE);
                llTagFlow3.setVisibility(VISIBLE);
                llTagFlow4.setVisibility(GONE);
                //第一种rv
                initDialogShopflowRv(headerTvShopFlow, rvflow);

                shopFlowAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        setZeroEtNum(ivCartAdd);


//                        ToastUtils.show("点击了" + shopFlowAdapter.getData().get(position).getSpecValue());
                        //设置点中的item
                        shopFlowAdapter.setThisPosition(position);
                        //将选中的数值
                        checkPosition = position;
                        //拿到当前的值
                        GoodsInfoBean.Data.GoodsSpecs.SpecValue oneList = shopFlowAdapter.getData().get(position);
                        //"款式":"字母格"
                        specValue = "{" + "\"" + oneList.getKey() + "\"" + ":" + "\"" + oneList.getValue() + "\"" + ",";

                        checkPositionShop(data, ivCartGoodsImg, tvGoodsTotalPrice, tvGoodsSave, tvCartSkuSpec1, cartAddbtn, btnName, llAddBuy);
                        //切换的数据
                        if (!TextUtils.isEmpty(etCartGoodsNum.getText().toString()) && Integer.parseInt(etCartGoodsNum.getText().toString()) > allSkuNumber) {
                            etCartGoodsNum.setText(allSkuNumber + "");
                        }

                    }
                });

                //第二种的方式显示列表数据
                initDialogShopflowRv2(headerTvShopFlow2, rvflow2);

                shopFlowAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                        ToastUtils.show("点击了" + position);
                        //空 或者输入为0的时候
                        setZeroEtNum(ivCartAdd);

                        //设置点中的item
                        shopFlowAdapter2.setThisPosition(position);

                        checkPosition2 = position;

                        GoodsInfoBean.Data.GoodsSpecs.SpecValue goodList2 = shopFlowAdapter2.getData().get(position);

                        specValue2 = "\"" + goodList2.getKey() + "\"" + ":" + "\"" + goodList2.getValue() + "\"" + ",";

                        checkPositionShop(data, ivCartGoodsImg, tvGoodsTotalPrice, tvGoodsSave, tvCartSkuSpec1, cartAddbtn, btnName, llAddBuy);

                        //切换的数据
                        if (!TextUtils.isEmpty(etCartGoodsNum.getText().toString()) && Integer.parseInt(etCartGoodsNum.getText().toString()) > allSkuNumber) {
                            etCartGoodsNum.setText(allSkuNumber + "");
                        }

                    }
                });


                initDialogShopflowRv3(headerTvShopFlow3, rvflow3);

                shopFlowAdapter3.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        setZeroEtNum(ivCartAdd);

                        //设置点中的item
                        shopFlowAdapter3.setThisPosition(position);

                        checkPosition3 = position;

                        GoodsInfoBean.Data.GoodsSpecs.SpecValue goodList3 = shopFlowAdapter3.getData().get(position);

                        specValue3 = "\"" + goodList3.getKey() + "\"" + ":" + "\"" + goodList3.getValue() + "\"" + "}";

                        checkPositionShop(data, ivCartGoodsImg, tvGoodsTotalPrice, tvGoodsSave, tvCartSkuSpec1, cartAddbtn, btnName, llAddBuy);

                        //切换的数据
                        if (!TextUtils.isEmpty(etCartGoodsNum.getText().toString()) && Integer.parseInt(etCartGoodsNum.getText().toString()) > allSkuNumber) {
                            etCartGoodsNum.setText(allSkuNumber + "");
                        }
                    }
                });
            }


            //加入购物车
            cartAddbtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });

            //立即购买
            cartBuybtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Integer.parseInt(etCartGoodsNum.getText().toString()) <= 0) {
                        ToastUtils.show("请输入购买数量~");
                        return;
                    }
                    Intent intent = new Intent(mContext, ComfirmOrderActivity.class);
                    Bundle bundle = new Bundle();
                    OrderGoodsList orderGoodsList = new OrderGoodsList();
                    orderGoodsList.setOrderGoodsListData(getOrderGoodsList1(Integer.parseInt(etCartGoodsNum.getText().toString())));
                    bundle.putSerializable(ComfirmOrderActivity.ORDERGOODSLIST, orderGoodsList);
                    intent.putExtra(ComfirmOrderActivity.ORDERTYPE, data.getData().getShopsTypeId());
                    intent.putExtra(ComfirmOrderActivity.TENANTID, data.getData().getTenantId());
                    intent.putExtra(ComfirmOrderActivity.ADDRESSID, "");
                    intent.putExtras(bundle);
                    mContext.startActivity(intent);
                }
            });

            ivDialogDelete.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    smartDismiss();
                    KeyboardUtils.hideKeyboard(etCartGoodsNum);

                }
            });


            //减少数据点击事件
            rlCartMinus.setOnClickListener(v -> ivMinNumber(etCartGoodsNum, ivCartMinus, ivCartAdd, rlCartAdd, rlCartMinus));

            //增加数据点击事件
            rlCartAdd.setOnClickListener(v -> ivAddNumber(etCartGoodsNum, ivCartMinus, ivCartAdd, rlCartAdd, rlCartMinus));

            //监听文字修改
            etCartGoodsNum.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (s.length() > 1) {

                        if (Integer.parseInt(s.toString()) == 0) {
                            return;
                        }

                        if (Integer.parseInt(s.toString()) > allSkuNumber) {

                            LogUtils.e("edit------", "重复刷新");
                            etCartGoodsNum.setText(allSkuNumber + "");
                            ivCartAdd.setBackgroundResource(R.drawable.input_add_disabled);
                            rlCartAdd.setEnabled(false);
                            //大于库存可以减
                            ivCartMinus.setBackgroundResource(R.drawable.input_minus_default);
                            ivCartMinus.setEnabled(true);

                            if (allSkuNumber == 0) {
                                ivCartMinus.setBackgroundResource(R.drawable.input_minus_disabled);
                                rlCartMinus.setEnabled(false);
                                ivCartAdd.setBackgroundResource(R.drawable.input_add_disabled);
                                rlCartAdd.setEnabled(false);
                            }

                        } else if (Integer.parseInt(s.toString()) <= 0 || s.toString().startsWith("0")) {
                            etCartGoodsNum.setText("1");
                            LogUtils.e("edit------小于0", "重复刷新");
                        }

                    }

                }

                @Override
                public void afterTextChanged(Editable s) {


                }
            });

            //判断库存有数据 第一次弹窗 显示的时候
            if (data.getData().getGoodsSpecs().size() > 0 && data.getData().getGoodsSpecs().size() == 1) {
                tvCartSkuSpec1.setVisibility(VISIBLE);

                //循环去比较 取出对应的库存的数量
                GoodsInfoBean.Data.GoodsSpecs.SpecValue goodsSpecList = specValueList.get(checkPosition);
                //取到当前默认值
                specValue = "{" + "\"" + goodsSpecList.getKey() + "\"" + ":" + "\"" + goodsSpecList.getValue() + "\"" + "}";
                LogUtils.v("购物车第一次弹窗的时候" + specValue);
                //总库存
                for (int j = 0; j < data.getData().getGoodsSkus().size(); j++) {
                    if (data.getData().getGoodsSkus().get(j).getSpec().contains(specValue)) {
                        checkValue = data.getData().getGoodsSkus().get(j).getSpec();
                        checkPosition = j;
                        LogUtils.v("购物车第一次弹窗的时候当前的值---" + checkValue + "当前的位置--" + checkPosition);
                    }

                }
                /*记录弹窗选择时的位置*/
                shopFlowAdapter.setThisPosition(checkPosition);
                shopFlowAdapter.notifyDataSetChanged();
                //显示图片中的当前选择的商品
                isChoosePosition(data, ivCartGoodsImg, tvGoodsTotalPrice, tvGoodsSave, tvCartSkuSpec1,
                        cartAddbtn, btnName, checkPosition, llAddBuy);


            } else if (data.getData().getGoodsSpecs().size() > 0 && data.getData().getGoodsSpecs().size() == 2) {
                //第一次弹窗两种选择的时候
                tvCartSkuSpec1.setVisibility(View.VISIBLE);
                //
                LogUtils.v("再次弹出窗口---" + checkPosition + "第二种弹出的位置" + checkPosition2);

                GoodsInfoBean.Data.GoodsSpecs.SpecValue goodsSpecList = specValueList.get(checkPosition);
                //取到当前默认值
                specValue = "{" + "\"" + goodsSpecList.getKey() + "\"" + ":" + "\"" + goodsSpecList.getValue() + "\"" + "}";

                GoodsInfoBean.Data.GoodsSpecs.SpecValue goodsSpecList2 = specValueList2.get(checkPosition2);

                specValue2 = "\"" + goodsSpecList2.getKey() + "\"" + ":" + "\"" + goodsSpecList2.getValue() + "\"" + "}";

                // 拿到拼接的值去总库存搜索
//                LogUtils.v("拼接的值" +specValue + specValue2);
                LogUtils.v("购物车第一次弹窗的时候" + specValue + specValue2);
                //总库存
                int allchecksku = 0;
                for (int j = 0; j < data.getData().getGoodsSkus().size(); j++) {
                    if (data.getData().getGoodsSkus().get(j).getSpec().equals(specValue + specValue2)) {
                        checkValue = data.getData().getGoodsSkus().get(j).getSpec();
                        allchecksku = j;
                        LogUtils.v("购物车第一次弹窗的时候当前的值2---" + checkValue + "当前的位置2--" + allchecksku);

                    }
                }

                /*记录弹窗选择时的位置*/
                shopFlowAdapter.setThisPosition(checkPosition);
                shopFlowAdapter.notifyDataSetChanged();
                /*记录弹窗选择时的位置*/
                LogUtils.v("循环结束后当前的值2---" + checkValue + "当前的位置2--" + checkPosition2);
                shopFlowAdapter2.setThisPosition(checkPosition2);
                shopFlowAdapter2.notifyDataSetChanged();
                //第一次弹窗选择项
                isChoosePosition(data, ivCartGoodsImg, tvGoodsTotalPrice, tvGoodsSave, tvCartSkuSpec1,
                        cartAddbtn, btnName, allchecksku, llAddBuy);

            } else if (data.getData().getGoodsSpecs().size() == 3) {
                //第三种情况

                //第一次弹窗两种选择的时候
                tvCartSkuSpec1.setVisibility(View.VISIBLE);
                //
                LogUtils.v("再次弹出窗口---" + checkPosition + "第三种弹出的位置" + checkPosition2);

                GoodsInfoBean.Data.GoodsSpecs.SpecValue goodsSpecList = specValueList.get(checkPosition);
                //取到当前默认值
                specValue = "{" + "\"" + goodsSpecList.getKey() + "\"" + ":" + "\"" + goodsSpecList.getValue() + "\"" + ",";

                GoodsInfoBean.Data.GoodsSpecs.SpecValue goodsSpecList2 = specValueList2.get(checkPosition2);

                specValue2 = "\"" + goodsSpecList2.getKey() + "\"" + ":" + "\"" + goodsSpecList2.getValue() + "\"" + ",";

                GoodsInfoBean.Data.GoodsSpecs.SpecValue goodsSpecList3 = specValueList3.get(checkPosition3);

                specValue3 = "\"" + goodsSpecList3.getKey() + "\"" + ":" + "\"" + goodsSpecList3.getValue() + "\"" + "}";
                // 拿到拼接的值去总库存搜索
//                LogUtils.v("拼接的值" +specValue + specValue2);
                LogUtils.v("购物车第一次弹窗的时候" + specValue + specValue2 + specValue3);
                //总库存
                int allchecksku = 0;
                for (int j = 0; j < data.getData().getGoodsSkus().size(); j++) {
                    if (data.getData().getGoodsSkus().get(j).getSpec().equals(specValue + specValue2 + specValue3)) {
                        checkValue = data.getData().getGoodsSkus().get(j).getSpec();
                        allchecksku = j;
                        LogUtils.v("购物车第一次弹窗的时候当前的值2---" + checkValue + "当前的位置2--" + allchecksku);

                    }
                }

                /*记录弹窗选择时的位置*/
                shopFlowAdapter.setThisPosition(checkPosition);
                shopFlowAdapter.notifyDataSetChanged();
                /*记录弹窗选择时的位置*/
                LogUtils.v("循环结束后当前的值2---" + checkValue + "当前的位置2--" + checkPosition2);
                shopFlowAdapter2.setThisPosition(checkPosition2);
                shopFlowAdapter2.notifyDataSetChanged();

                shopFlowAdapter3.setThisPosition(checkPosition3);
                shopFlowAdapter3.notifyDataSetChanged();
                //第一次弹窗选择项 三种情况
                isChoosePosition(data, ivCartGoodsImg, tvGoodsTotalPrice, tvGoodsSave, tvCartSkuSpec1,
                        cartAddbtn, btnName, allchecksku, llAddBuy);


            } else if (data.getData().getInventory() == 0) {
                llAddBuy.setVisibility(GONE);
                //如果没有列表 选择就是详情的总库存
                allSkuNumber = data.getData().getInventory();
                GlideUtils.setImageView(mContext, data.getData().getGoodsImg(), ivCartGoodsImg, 8);
                tvGoodsTotalPrice.setText("¥" + data.getData().getGoodsPrice());
                tvGoodsSave.setText("库存：" + data.getData().getInventory() + "件");
            } else {
                llAddBuy.setVisibility(VISIBLE);
                //如果没有列表 选择就是详情的总库存
                allSkuNumber = data.getData().getInventory();
                GlideUtils.setImageView(mContext, data.getData().getGoodsImg(), ivCartGoodsImg, 8);
                tvGoodsTotalPrice.setText("¥" + data.getData().getGoodsPrice());
                tvGoodsSave.setText("库存：" + data.getData().getInventory() + "件");

            }


        }


        //设置为库存为0的逻辑判断
        private void setZeroEtNum(ImageView ivCartAdd) {
            if (TextUtils.isEmpty(etCartGoodsNum.getText().toString()) | etCartGoodsNum.getText().toString().startsWith("0")) {
                etCartGoodsNum.setText("1");
                ivCartAdd.setBackgroundResource(R.drawable.input_add_default);
                ivCartAdd.setEnabled(true);

            }
        }


        @Override
        protected void onShow() {
            super.onShow();

        }

        // 设置最大宽度，看需要而定，
        @Override
        protected int getMaxWidth() {
            return super.getMaxWidth();
        }

        // 设置最大高度，看需要而定
        @Override
        protected int getMaxHeight() {
            return super.getMaxHeight();
        }

        // 设置自定义动画器，看需要而定
        @Override
        protected PopupAnimator getPopupAnimator() {
            return super.getPopupAnimator();
        }

        /**
         * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
         *
         * @return
         */
        protected int getPopupWidth() {
            return 0;
        }

        /**
         * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
         *
         * @return
         */
        protected int getPopupHeight() {
            return 0;
        }


        public String getBtnName() {
            return btnName;
        }

        public void setBtnName(String btnName) {
            this.btnName = btnName;
        }

        protected String btnName;

        @Override
        protected void onDismiss() {
            super.onDismiss();

            dismissWith(new Runnable() {
                @Override
                public void run() {
                    KeyboardUtils.hideKeyboard(etCartGoodsNum);
                }
            });

        }


        @Override
        public void onItemClick(String value, int position) {
            ToastUtils.show(value);

            tvCartSkuSpec1.setText(value);
        }


        /*规格1*/
        private void initDialogShopflowRv(TextView tvHeaderShop1, RecyclerView rvFlow) {
            if (!data.getData().getGoodsSpecs().get(0).getSpecKey().isEmpty()) {
                tvHeaderShop1.setVisibility(VISIBLE);
                tvHeaderShop1.setText(data.getData().getGoodsSpecs().get(0).getSpecKey());
            }

            //2，增加谷歌流式布局
            FlexboxLayoutManager manager = new FlexboxLayoutManager(mContext, FlexDirection.ROW, FlexWrap.WRAP) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            rvFlow.setLayoutManager(manager);

            specValueList = new ArrayList<>();

            //抽取相同的逻辑判断
            addSpecLists(0, specValueList);

            shopFlowAdapter = new ShopFlowItemAdapter(mContext, specValueList);

            rvFlow.setAdapter(shopFlowAdapter);

            shopFlowAdapter.notifyDataSetChanged();


        }


        /*规格2*/
        private void initDialogShopflowRv2(TextView tvHeaderShop1, RecyclerView rvFlow) {
            if (!data.getData().getGoodsSpecs().get(1).getSpecKey().isEmpty()) {
                tvHeaderShop1.setVisibility(VISIBLE);
                tvHeaderShop1.setText(data.getData().getGoodsSpecs().get(1).getSpecKey());
            }
            //2，增加谷歌流式布局
            FlexboxLayoutManager manager = new FlexboxLayoutManager(mContext, FlexDirection.ROW, FlexWrap.WRAP) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            rvFlow.setLayoutManager(manager);
            specValueList2 = new ArrayList<>();

            addSpecLists(1, specValueList2);

            shopFlowAdapter2 = new ShopFlowItemAdapter2(mContext, specValueList2);

            rvFlow.setAdapter(shopFlowAdapter2);

            shopFlowAdapter2.notifyDataSetChanged();


        }

        //设置规格的逻辑判断
        private void addSpecLists(int position, List<GoodsInfoBean.Data.GoodsSpecs.SpecValue> specValueList2) {
            if (data.getData().getGoodsSpecs().get(position).getSpecValue().contains(",")) {
                String[] split = data.getData().getGoodsSpecs().get(position).getSpecValue().split(",");

                for (int i = 0; i < split.length; i++) {
                    //颜色和类别
                    GoodsInfoBean.Data.GoodsSpecs.SpecValue specBean = new GoodsInfoBean.Data.GoodsSpecs.SpecValue(data.getData().getGoodsSpecs().get(position).getSpecKey(), split[i]);
                    specValueList2.add(specBean);
                }
            } else {
                GoodsInfoBean.Data.GoodsSpecs.SpecValue specBean = new GoodsInfoBean.Data.GoodsSpecs.SpecValue(data.getData().getGoodsSpecs().get(position).getSpecKey(), data.getData().getGoodsSpecs().get(position).getSpecValue());
                specValueList2.add(specBean);
            }
        }

        /*规格3*/
        private void initDialogShopflowRv3(TextView tvHeaderShop1, RecyclerView rvFlow) {

            if (!data.getData().getGoodsSpecs().get(2).getSpecKey().isEmpty()) {
                tvHeaderShop1.setVisibility(VISIBLE);
                tvHeaderShop1.setText(data.getData().getGoodsSpecs().get(2).getSpecKey());
            }
            //2，增加谷歌流式布局
            FlexboxLayoutManager manager = new FlexboxLayoutManager(mContext, FlexDirection.ROW, FlexWrap.WRAP) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            rvFlow.setLayoutManager(manager);
            specValueList3 = new ArrayList<>();

            addSpecLists(2, specValueList3);

            shopFlowAdapter3 = new ShopFlowItemAdapter3(mContext, specValueList3);

            rvFlow.setAdapter(shopFlowAdapter3);

            shopFlowAdapter3.notifyDataSetChanged();


        }
    }


    /**
     * @description 一种商品的选项情况
     */
    //第一种的
    String specValue;
    //第二种的
    String specValue2;
    //第三种
    String specValue3;
    //总的文字
    String checkValue;
    //记得adapter的位置1
    int checkPosition = 0;
    //记录adadter 的位置2
    int checkPosition2 = 0;

    int checkPosition3 = 0;

    private int allSkuNumber;

    private String allSkuId = "0";

    //增加减少的变量
    private int etAddNumber;

    private void checkPositionShop(GoodsInfoBean data, ImageView imageView, TextView tvPrice, TextView tvGoodsSave,
                                   TextView tvCartSkuSpec1, Button button, String btnName, LinearLayout lladdbuy) {

        //判断库存有数据 第一次弹窗 显示的时候
        if (data.getData().getGoodsSpecs().size() > 0 && data.getData().getGoodsSpecs().size() == 1) {
            tvCartSkuSpec1.setVisibility(View.VISIBLE);
            //循环去比较 取出对应的库存的数量
            //取到当前默认值
            //总库存选择的位置1
            for (int j = 0; j < data.getData().getGoodsSkus().size(); j++) {
                if (data.getData().getGoodsSkus().get(j).getSpec().contains(specValue)) {
                    checkValue = data.getData().getGoodsSkus().get(j).getSpec();
                    checkPosition = j;
                    LogUtils.v("当前的值---" + checkValue + "当前的位置--" + checkPosition);

                }
            }
            //选择库存所有id
            isChoosePosition(data, imageView, tvPrice, tvGoodsSave, tvCartSkuSpec1, button, btnName, checkPosition, lladdbuy);

        } else if (data.getData().getGoodsSpecs().size() > 0 && data.getData().getGoodsSpecs().size() == 2) {

            tvCartSkuSpec1.setVisibility(View.VISIBLE);

            LogUtils.v("拼接的值" + specValue + specValue2);
            //总库存
            int allSkuposition2 = 0;
            for (int j = 0; j < data.getData().getGoodsSkus().size(); j++) {
                if (data.getData().getGoodsSkus().get(j).getSpec().equals(specValue + specValue2)) {
                    checkValue = data.getData().getGoodsSkus().get(j).getSpec();
//                    checkPosition2 = j;
                    allSkuposition2 = j;
                    LogUtils.v("循环查找当前的值2---" + checkValue + "循环查询当前的位置2--" + checkPosition2);

                }
            }
            isChoosePosition(data, imageView, tvPrice, tvGoodsSave, tvCartSkuSpec1, button, btnName, allSkuposition2, lladdbuy);
        } else if (data.getData().getGoodsSpecs().size() == 3) {

            tvCartSkuSpec1.setVisibility(View.VISIBLE);

            LogUtils.v("拼接的值" + specValue + specValue2 + specValue3);
            //总库存
            int allSkuposition2 = 0;
            for (int j = 0; j < data.getData().getGoodsSkus().size(); j++) {
                if (data.getData().getGoodsSkus().get(j).getSpec().equals(specValue + specValue2 + specValue3)) {
                    checkValue = data.getData().getGoodsSkus().get(j).getSpec();
                    allSkuposition2 = j;
                    LogUtils.v("循环查找当前的值2---" + checkValue + "循环查询当前的位置2--" + checkPosition3);

                }
            }
            isChoosePosition(data, imageView, tvPrice, tvGoodsSave, tvCartSkuSpec1, button, btnName, allSkuposition2, lladdbuy);
        }

    }

    /**
     * @description 抽取公共部分 总库存去找下的位置
     */

    private void isChoosePosition(GoodsInfoBean data, ImageView imageView, TextView tvPrice, TextView tvGoodsSave,
                                  TextView tvCartSkuSpec1, Button button, String btnName, int allcheckPosition, LinearLayout lladdbuy) {

        allSkuId = data.getData().getGoodsSkus().get(allcheckPosition).getId();
        //第一 第二种弹窗库存的总数
        allSkuNumber = data.getData().getGoodsSkus().get(allcheckPosition).getInventory();

        if (data.getData().getGoodsSkus().get(allcheckPosition).getSpecImg().isEmpty()) {
            GlideUtils.setImageView(mContext, data.getData().getGoodsImg(), imageView, 8);
            tvPrice.setText("¥" + data.getData().getGoodsSkus().get(allcheckPosition).getGoodsPrice());
            tvGoodsSave.setText("库存：" + data.getData().getGoodsSkus().get(allcheckPosition).getInventory() + "件");

            String datas = data.getData().getGoodsSkus().get(allcheckPosition).getSpec().replace("{", "")
                    .replace("}", "").replaceAll("\"", "");

//            SpecJson specJson = new Gson().fromJson(data.getData().getGoodsSkus().get(allcheckPosition).getSpec(), SpecJson.class);

            tvCartSkuSpec1.setText("已选：" + datas);

            setInventory(data, button, btnName, allcheckPosition, lladdbuy);
            //弹窗选择


        } else {
            //的情况
            GlideUtils.setImageView(mContext, data.getData().getGoodsSkus().get(allcheckPosition).getSpecImg(), imageView, 8);
            tvPrice.setText("¥" + Utils.formatZeroNumber(data.getData().getGoodsSkus().get(allcheckPosition).getGoodsPrice()));
            tvGoodsSave.setText("库存：" + data.getData().getGoodsSkus().get(allcheckPosition).getInventory() + "件");

            String datas = data.getData().getGoodsSkus().get(allcheckPosition).getSpec().replace("{", "")
                    .replace("}", "").replaceAll("\"", "");

//            SpecJson specJson = new Gson().fromJson(data.getData().getGoodsSkus().get(allcheckPosition).getSpec().replaceAll("\"", ""), SpecJson.class);

//            LogUtils.e(new Gson().toJson(data.getData().getGoodsSkus().get(allcheckPosition).getSpec().replaceAll("\"", "")));

            tvCartSkuSpec1.setText("已选：" + datas);

            //弹窗选择
            //设置库存
            setInventory(data, button, btnName, allcheckPosition, lladdbuy);

        }
    }
    /**/

    private void setInventory(GoodsInfoBean data, Button button, String btnName, int allcheckPosition, LinearLayout lladdbuy) {
        if (data.getData().getGoodsSkus().get(allcheckPosition).getInventory() == 0) {
            lladdbuy.setVisibility(View.GONE);
        } else {
            lladdbuy.setVisibility(View.VISIBLE);
        }
    }


    /**
     * @description 加商品
     */

    private void ivAddNumber(EditText etCartGoodsNum, ImageView ivCartMinus, ImageView ivCartAdd, RelativeLayout rlCartAdd, RelativeLayout rlCartMin) {
        String etNumber = etCartGoodsNum.getText().toString();
        //空 或者输入为0的时候
        if (etNumber.startsWith("0")) {
            etCartGoodsNum.setText("0");
        } else if (TextUtils.isEmpty(etNumber)) {
            etCartGoodsNum.setText("0");
        }
        etAddNumber = Integer.parseInt(etCartGoodsNum.getText().toString());
        etAddNumber = etAddNumber + 1;
        etCartGoodsNum.setText(String.format("" + etAddNumber));
        String etAddfinish = etCartGoodsNum.getText().toString();


        //判断数据大于且小于库存
        if (Integer.parseInt(etAddfinish) > 1 && Integer.parseInt(etAddfinish) < allSkuNumber) {//数量加减显示
            ivCartMinus.setBackground(getResources().getDrawable(R.drawable.input_minus_default));
            ivCartMinus.setEnabled(true);
            ivCartAdd.setBackgroundResource(R.drawable.input_add_default);
            ivCartAdd.setEnabled(true);


        } else if (Integer.parseInt(etAddfinish) == 1) {
            ivCartMinus.setBackgroundResource(R.drawable.input_minus_disabled);
            ivCartMinus.setEnabled(false);
            ivCartAdd.setBackgroundResource(R.drawable.input_add_default);
            ivCartAdd.setEnabled(true);
        } else {
//            ToastUtils.show("+++");
            ivCartAdd.setBackgroundResource(R.drawable.input_add_disabled);
            ivCartAdd.setEnabled(false);
            //大于库存可以减
            ivCartMinus.setBackgroundResource(R.drawable.input_minus_default);
            ivCartMinus.setEnabled(true);
            //设置为最大库存数
            etCartGoodsNum.setText(data.getData().getInventory() + "");
        }
    }

    /**
     * @description 减商品
     */

    private void ivMinNumber(EditText etCartGoodsNum, ImageView ivCartMinus, ImageView ivCartAdd, RelativeLayout rlCartAdd, RelativeLayout rlCartMin) {

        //点击减少后都可以减
        rlCartAdd.setEnabled(true);
        String etNumber = etCartGoodsNum.getText().toString();
        //空 或者输入为0的时候
        if (TextUtils.isEmpty(etNumber) || etNumber.startsWith("0")) {
            etCartGoodsNum.setText("1");
        }
        etAddNumber = Integer.parseInt(etNumber);
        etAddNumber = etAddNumber - 1;
        etCartGoodsNum.setText(String.format("" + etAddNumber));
        String etdownfinish = etCartGoodsNum.getText().toString();

        if (Integer.parseInt(etdownfinish) <= 1) {
            ivCartMinus.setBackgroundResource(R.drawable.input_minus_disabled);
            ivCartMinus.setEnabled(false);
            etCartGoodsNum.setText("1");
            LogUtils.v("减少的数---" + etdownfinish);
            return;
        }
        if (Integer.parseInt(etdownfinish) >= data.getData().getInventory()) {
            //大于等于库存

            ivCartAdd.setBackgroundResource(R.drawable.input_add_disabled);
            ivCartAdd.setEnabled(false);
            //小于1
            ivCartMinus.setBackgroundResource(R.drawable.input_minus_default);
            ivCartMinus.setEnabled(true);
            etCartGoodsNum.setText(data.getData().getInventory() + "");
        } else {
            //
            ivCartAdd.setBackgroundResource(R.drawable.input_add_default);
            ivCartAdd.setEnabled(true);

        }
    }


    //*******************商品详情加入购物车********************************//
}
